perm filename A46[106,RWF] blob sn#790300 filedate 1985-03-11 generic text, type C, neo UTF8
COMMENT ⊗   VALID 00003 PAGES
C REC  PAGE   DESCRIPTION
C00001 00001
C00002 00002	¬This Could Be the Start of Something Big_ (Assignment)
C00007 00003
C00012 ENDMK
C⊗;
¬This Could Be the Start of Something Big_ (Assignment)

I want to design a program which reads an integer N, followed by a list of
N numbers. The program must print the first number, the last number, and
the length, of the longest consecutive run of increasing numbers. That is,
the program can help me say fascinating things like ``The temperature rose
steadily, from 28 to 103, for 19 consecutive days. That's still a record.''

To prepare for programming the above problem, think about it this way.
If I were watching the consecutive data go by on a television screen,
and recording information on a very small blackboard, what information
would I keep that would guarantee that I could answer the question the
program is designed to answer?

I must record the starting value, final value, and length of the longest
run I have seen so far, because the numbers I have not yet seen might
steadily decrease. That is not enough to record, though; if I am currently
in the middle of a run which will eventually be seen to be the longest so
far, I will need to know its starting value and how long it was, so I
need to keep the starting value and length so far; even if the number I
am currently looking at is smaller than the one before, it might be the
start of a long run. I also need to remember the last number of the 
current run, to see if the next number continues it.

_Drill_: Check that no more information is needed.

The main iteration is then:

FOR I:= 1 TO N DO
	BEGIN
	READ(NEWDAT);
	IF NEWDAT > LAST THEN
		BEGIN
		LAST:= NEWDAT;
		LENGTH:= LENGTH + 1
		END
	ELSE
		BEGIN
		FIRST:= NEWDAT;
		LAST:= NEWDAT
		LENGTH:= 1
		END;
	IF LENGTH > OLDLENGTH THEN
		OLDFIRST:= FIRST;
		OLDLENGTH:= LENGTH;
		OLDLAST:= LAST
	END.


After the first datum is read, we want to have FIRST, LAST, OLDFIRST and
OLDLAST equal to that datum, and LENGTH and OLDLENGTH equal to 1. 
The main iteration will do this if I initialize LAST to the largest 
possible number, and OLDLENGTH to 0.

The program then is

LAST:= MAXINT;
OLDLENGTH:= 0
(* Insert main iteration here *)
WRITE(OLDLENGTH, OLDFIRST, OLDLAST)

The variable OLDLENGTH in the program above is an example of what I call a
_ratchet_; it can only move one way because it is changed only to a value
known to be larger than its previous value. If initialized to a small
enough value, a ratchet keeps track of the largest value ever presented
to it. Often, as in the example, it is accompanied by other variables
that record information about the context in which the ratchet was last
changed.